//
//  ViewController.swift
//  Swift_01of30
//
//  Created by licheng ke on 2017/2/27.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit
import Foundation


class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource , CAAnimationDelegate {
    
    var viewMask : CALayer! = CALayer();
    var isFirstInit = true;
    var tableView : UITableView!
    var days = ["timer count" ,"custom font","playLocalVideo",
                "snapChat","Carousel","Where",
                "PullAToRefreshh","RandomColorMusic","ImageScroller","VideoBackground","ClearTableViewCell","LoginAnimation","AnimatabletableView","EmojiSpin","Splash",
                "SlideMeun","TumblrMenu","LimitCharactors",
                "PullRefresh","CollectionViewAnimation","SwipeCell",
                "3Dtouch","revealController","CustomLayoutCollectionView"
                ];
    
    override func viewDidLoad() {
        super.viewDidLoad();
        //设置启动动画
        self.view.backgroundColor = UIColor.blue;
        let wappedView = UIView(frame: UIScreen.main.bounds);
        wappedView.backgroundColor = .black;
        self.view.addSubview(wappedView);
        //配置其他的UI
        configUI(wap:wappedView);
        
        //订制导航栏
        self.navigationController?.navigationBar.tintColor = UIColor.darkGray;
        let barButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil);
        self.navigationItem.backBarButtonItem = barButtonItem;
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        checkShortCutItemData();
    }
    
    func checkShortCutItemData(){
        let delegate = UIApplication.shared.delegate as! AppDelegate
        if let title = delegate.item {
            print("checkShortCutItemData : \(title)")
            self.title = title
        }
    }
    
    func configUI(wap myView:UIView) {
        
        tableView = UITableView();
        myView.addSubview(tableView);
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = UIColor.clear;
        tableView.tableFooterView = UIView();
        tableView.snp.makeConstraints { maker in
            //topLayoutGuide 的 top 其实就是view的top，scrollview是设置了他自己的contentInset[]
//            maker.top.equalTo(self.topLayoutGuide.snp.top);
            maker.edges.equalTo(0);
//            maker.bottom.equalTo(self.bottomLayoutGuide.snp.top);
        }

        viewMask.contents = #imageLiteral(resourceName: "twitter").cgImage;
        viewMask.frame = CGRect(x: 0, y: 0, width: 138, height: 112);
        viewMask.anchorPoint = CGPoint(x: 0.5, y: 0.5);
        viewMask.position = view.center;
        myView.layer.mask = viewMask;
    }
    
//MARK: - dataSource & delegate
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        //这里为什么不用带indexpath的方法，因为那个是一定返回cell的，所以不可能判断cell是否是nil
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "abc");
        if cell == nil {
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "abc");
            cell?.textLabel?.textColor = UIColor.white;
            cell?.detailTextLabel?.textColor = UIColor.orange;
//            cell?.contentView.backgroundColor = UIColor.clear;
            cell?.backgroundColor = UIColor.clear;
            cell?.separatorInset = UIEdgeInsets.zero;
        }
        cell?.textLabel?.text = "Day \(indexPath.row + 1)";
        cell?.detailTextLabel?.text = days[indexPath.row];
        return cell!;
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return days.count;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true);
        
        if indexPath.row == 17 {
            let controller = Controller18(nibName: "Controller18", bundle: nil)
            controller.title = days[indexPath.row];
            navigationController?.pushViewController(controller, animated: true);
            return
        }
        
        
        let ModuleName = g_getProjectName() + ".Controller\((indexPath.row + 1).formatString(format: "02"))";
        let ControllerClass = NSClassFromString(ModuleName) as! UIViewController.Type ;
        let controller = ControllerClass.init();
        controller.title = days[indexPath.row];
        navigationController?.pushViewController(controller, animated: true);
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated);
        if self.isFirstInit {  //如果使用shortcut 进入这里要设置一下不是第一次进入了，不让做动画
            launchAnimation();
            self.isFirstInit = false;
        }
    }
 
    func launchAnimation() {
        let toBounds = CGRect(x: 0, y: 0, width: 2300, height: 2000);
        CATransaction.begin()
        CATransaction.setDisableActions(true);
        
        let animation = CABasicAnimation(keyPath: "bounds")
        animation.delegate = self;
        animation.fromValue = CGRect(x: 0, y:0 , width: 138, height: 112);
        animation.toValue = toBounds;
        animation.duration = 1.0;
        viewMask.bounds = toBounds;
        viewMask.add(animation, forKey: "frame")
        CATransaction.commit()
        
    }
    
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
        viewMask.removeFromSuperlayer();
        viewMask = nil;
    }
    
}













